﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;

namespace ZHONGLANGWORD.WebOffice
{
    public class PUBWORD
    {
        #region 文本替换
        public static string proMarkWord(SortedList SDList, string strLocPath)
        {
            string strBack = "";
            Microsoft.Office.Interop.Word.Application app = null;
            Microsoft.Office.Interop.Word.Document doc = null;
            object unknow = Type.Missing;
            object objLocPath = strLocPath;
            try
            {
                app = new Microsoft.Office.Interop.Word.Application(); ;
                doc = app.Documents.Open(ref objLocPath, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
                        ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);

                #region MarkUpdate
                //if (doc.Bookmarks.Exists("USERNAME")) doc.Bookmarks["USERNAME"].Range.Text = CURRUSERNAME;
                #endregion

                #region TextUpdate 
                foreach (KeyValuePair<string, string> item in SDList)
                {
                    //item.Key, item.Value  
                    app.Selection.Find.ClearFormatting();
                    app.Selection.Find.Replacement.ClearFormatting();
                    app.Selection.Find.Text = item.Key;
                    app.Selection.Find.Replacement.Text = item.Value;

                    object objReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
                    app.Selection.Find.Execute(ref unknow, ref unknow, ref unknow,
                                               ref unknow, ref unknow, ref unknow,
                                               ref unknow, ref unknow, ref unknow,
                                               ref unknow, ref objReplace, ref unknow,
                                               ref unknow, ref unknow, ref unknow);
                }
                #endregion

                doc.Save();
            }
            catch (Exception Ex) { strBack = "更新书签时错误:" + Ex.ToString(); }
            finally
            {
                try { if (doc != null) { doc.Close(ref unknow, ref unknow, ref unknow); } } catch { }
                try { if (app != null) { app.Quit(ref unknow, ref unknow, ref unknow); } } catch { }
            }
            return strBack;
        }
        #endregion

        #region 书签方式更换
        //object strPathNew = MapPath(strCurrTempFile);
        //    Word.Application app = null; Word.Document doc = null; object unknow = Type.Missing;
        //    try{app = new Microsoft.Office.Interop.Word.Application(); ;
        //        doc = app.Documents.Open(ref strPathNew, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
        //                ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);

        //        #region MarkUpdate
        //        if (doc.Bookmarks.Exists("USERNAME")) doc.Bookmarks["USERNAME"].Range.Text = CURRUSERNAME; 
        //        #endregion

        //        doc.Save();

        //        jsMessage("按模板生成报告成功，请加载显示！");

        //    }
        //    catch { jsMessage("更新模板出现错误！"); }
        //    finally {
        //        try { if (doc != null) { doc.Close(ref unknow, ref unknow, ref unknow); } } catch { }
        //        try { if (app != null) { app.Quit(ref unknow, ref unknow, ref unknow); } } catch { }
        //    } 
        #endregion

        #region 签名测试
        public static string proMarkTest(string strLocPath,string strMarkName,string strMarkPath)
        {
            string strBack = "";
            Microsoft.Office.Interop.Word.Application app = null;
            Microsoft.Office.Interop.Word.Document doc = null;
            object unknow = Type.Missing;
            object objLocPath = strLocPath;  
            try
            {
                app = new Microsoft.Office.Interop.Word.Application(); ;
                doc = app.Documents.Open(ref objLocPath, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
                        ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);

                #region mark 试验者: 校对者: 室主任: 试验日期:年　月　日 
                string strMan1 = "试验者", strMark1 = "试验者";

                #region markInfo
                Microsoft.Office.Interop.Word.Table table = doc.Paragraphs.Last.Range.Tables.Add(doc.Paragraphs.Last.Range, 1, 8);
                table.Borders.InsideColor = Microsoft.Office.Interop.Word.WdColor.wdColorWhite;
                table.Borders.OutsideColor = Microsoft.Office.Interop.Word.WdColor.wdColorWhite;
                //table.Borders.Enable = 1;
                //table.Borders.InsideColor = Word.WdColor.wdColorBlue;
                //table.Borders.OutsideColor = Word.WdColor.wdColorBlack;
                //table.Borders.OutsideLineWidth = Word.WdLineWidth.wdLineWidth025pt;
                for (int k = 1; k <= 8; k++) { table.Rows[1].Cells[k].Range.Font.Size = 8; }
                table.Rows[1].Cells[1].Range.Text = "试验者:";
                table.Rows[1].Cells[3].Range.Text = "校对者:";
                table.Rows[1].Cells[5].Range.Text = "室主任:";
                table.Rows[1].Cells[7].Range.Text = "试验日期:";
                 
                    Image markPic = CreateImage(strMarkName, true, 12);
                    markPic.Save(strMarkPath);

                    table.Rows[1].Cells[2].Range.InlineShapes.AddPicture(strMark1, false, true);
                    table.Rows[1].Cells[2].Range.InlineShapes[1].Width = 40;
                    table.Rows[1].Cells[2].Range.InlineShapes[1].Height = 30;
                
               // else { table.Rows[1].Cells[2].Range.Text = strMan1; } 

                table.Rows[1].Cells[8].Range.Text = DateTime.Now.ToString();
                #endregion

                #endregion

                doc.Save();
            }
            catch { strBack = "向WORD文件中写入签字信息时失败！"; } 
            finally
            {
                try { if (doc != null) { doc.Close(ref unknow, ref unknow, ref unknow); } } catch { }
                try { if (app != null) { app.Quit(ref unknow, ref unknow, ref unknow); } } catch { }
            }
            return strBack;
        }
        #endregion

        #region 签字图片
        /// <summary>
        /// 生成文字图片
        /// </summary>
        /// <param name="text"></param>
        /// <param name="isBold"></param>
        /// <param name="fontSize"></param>
        public static Image CreateImage(string text, bool isBold, int fontSize)
        {
            int wid = 40;
            int high = 20;
            Font font;
            if (isBold)
            {
                font = new Font("Arial", fontSize, FontStyle.Bold);

            }
            else
            {
                font = new Font("Arial", fontSize, FontStyle.Regular);

            }
            //绘笔颜色
            SolidBrush brush = new SolidBrush(Color.Black);
            StringFormat format = new StringFormat(StringFormatFlags.NoClip);
            Bitmap image = new Bitmap(wid, high);
            Graphics g = Graphics.FromImage(image);
            SizeF sizef = g.MeasureString(text, font, PointF.Empty, format);//得到文本的宽高
            int width = (int)(sizef.Width + 1);
            int height = (int)(sizef.Height + 1);
            image.Dispose();
            image = new Bitmap(width, height);
            g = Graphics.FromImage(image);
            g.Clear(Color.White);//透明

            RectangleF rect = new RectangleF(0, 0, width, height);
            //绘制图片
            g.DrawString(text, font, brush, rect);
            //释放对象
            g.Dispose();
            return image;
        }
        #endregion
    }
}